package com.ac.app.web.taglib;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.caac.core.config.Config;
import com.caac.core.dao.BaseDao;
import com.caac.core.util.DateUtils;
import com.ac.app.dm.PickRecord;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright AgileCentury 2008</p>
 * <p>Company: AgileCentury </p>
 * @author <a href="mailto:ac@agilecentury.com">Agile</a>
 * @version 1.0
 */

public class Hot1BrowseTag extends BodyTagSupport {
    private static final long serialVersionUID = 1L;
    private String showNum = "10";
    private String channelId;
    private String chClass;
    private String newsClass;
    private String newsTime;
    private String where;

    public Hot1BrowseTag() {
        super();
        this.chClass = "M_title2";
        this.newsClass = "announce";
        this.newsTime = "true";
        this.where ="";
    }

    public void release() {
        super.release();
        channelId = null;
        chClass = null;
        this.newsClass = null;
        this.newsTime = null;
        this.where = null;
        
    }

    public void setChannelId(String channelId) {
        this.channelId = channelId;
    }

    public void setChClass(String chClass) {
        this.chClass = chClass;
    }

    public void setNewsClass(String newsClass) {
        this.newsClass = newsClass;
    }

    public void setNewsTime(String newsTime) {
        this.newsTime = newsTime;
    }

    public String getChannelId() {
        return channelId;
    }

    public String getChClass() {
        return chClass;
    }

    public String getNewsClass() {
        return newsClass;
    }

    public String getNewsTime() {
        return newsTime;
    }

    public int doStartTag() throws JspException {
        return EVAL_BODY_INCLUDE;
    }

    public int doEndTag() throws JspException {
        StringBuffer results = new StringBuffer();
        String whereString = where+" 1=1 order by clk_count desc ";
        BaseDao dao = (BaseDao) Config.getInstance().getBean("baseDao");
        int iCount = Integer.parseInt(showNum);

        List dmList = dao.getObjects(PickRecord.class, whereString, 1, iCount);

        results.append("<table width='98%' border=0 cellspacing=0 cellpadding=0>");
        for (int i = 0; i < dmList.size(); i++) {
            PickRecord dm = (PickRecord) dmList.get(i);
            results.append("<tr><td width=30><img src=\"images/search%20index_r6_c3-n"+(i+1)+".gif\" width=\"25\" height=\"19\"></td><td><a href='/pkmg/pick/browsePkRecord.do?id=");
            results.append(dm.getId());
            results.append("' target='_blank'>");
            results.append(dm.getTitle());
            results.append("</td><td align=center>");
            results.append(getBgColor(dm.getState()));
            results.append("</td></tr>");
        }
        results.append("</table>");

        try {
            pageContext.getOut().print(results);
        } catch (IOException ex) {
            ex.printStackTrace();
            throw new JspException("Error - tag.getProperty : IOException ");
        }
        return EVAL_PAGE;
    }

    public String getShowNum() {
        return showNum;
    }

    public void setShowNum(String showNum) {
        this.showNum = showNum;
    }

    private String getNotTitle(String title) {
        if(title.indexOf(";")>0){
            return title;
        }
        if (title.length() > 14) {
            return title.substring(0, 14) + "...";
        }
        return title;
    }

    public String getWhere() {
        return where;
    }

    public void setWhere(String where) {
        this.where = where;
    }
    
    public String getBgColor(String state) {
        if ("1".equals(state)) {
            return "#CCFFCC";
        }else if("F".equals(state)) {
            return "#FFFFCC";
        }
        return "#FFFFFF";
    }
}
